package pl.pkolodziej.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormValidation {

	public static Boolean checkLogin(String s){
		return (s.indexOf("@")>-1);
	}
	
	public static Boolean checkPassword(String s, String re){
		return (s.length()>4 && s.equals(re));
	}
	
	public static Boolean checkCode(String code){
		if (code.length()==0)
			return false;
		if(code.indexOf("-")>-1 && code.split("-").length!=2)
			return false;
		Pattern p = Pattern.compile("[qmsdilkt]");
		String s = code.split("-").length==2 ? code.split("-")[1] : code.split("-")[0];
		for(char c : s.toCharArray()){
			Matcher match = p.matcher(""+c);
			if(!match.find()){
				return false;
			}
		}
		return true;
	}
	
	public static List<Integer> decryptUserCode(String code){
			
		String groupId = code.split("-").length==2 ? code.split("-")[0] : "none";
		char[] arrCode = code.split("-").length==2 ? code.split("-")[1].toCharArray() : code.toLowerCase().toCharArray();
		int id=0;
		if(groupId.equals("t1")){
			id=10;
		}else if(groupId.equals("t2")){
			id=20;
		}else if(groupId.equals("t3")){
			id=30;
		}else if(groupId.equals("c1")){
			id=40;
		}
		List<Integer> courseId = new ArrayList<Integer>();
		for(char c : arrCode){
			switch(c) {
				case 'q'	:	courseId.add(id+1); break;	
				case 'm'	:	courseId.add(id+2); break;	
				case 's'	:	courseId.add(id+3); break;	
				case 'd'	:	courseId.add(id+4); break;	
				case 'i'	:	courseId.add(id+5); break;	
				case 'l'	:	courseId.add(id+6); break;	
				case 'k'	:	courseId.add(id+7); break;	
				case 't'	:	courseId.add(id+8); break;	
				default		:	break;
			}
		}
		return courseId;
	}
	
}
